<?php

namespace App\Http\Controllers\VideoApi;

use App\Http\Config\ErrorCode;
use App\Models\video\BroadcasterModel;
use App\Models\video\SubscriptionModel;
use Illuminate\Http\Request;

class ChannelController extends BaseController
{
    public function __construct(Request $request)
    {
        $this->noCheckTokenAction = ['getBroadcasterList', 'getBroadcasterInfo', 'getVideoList']; //不校验openid
        parent::__construct($request);
    }

    // 获取up主列表
    public function getBroadcasterList(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $broadcasterData = BroadcasterModel::getList("id,name,avatar_id,fan_number", ["status" => 1, "mall_id" => $request->mallData["id"]], $request->page ?: 1, $request->pageSize ?: 20, "fan_number");
        foreach ($broadcasterData["list"] as &$item){
            $item["subscribe"] = 0; // 未订阅
            $item["remindStatus"] = 0; // 提醒状态
            if($request->token) {
                $userId = $this->getTokenVar($request->token, "id");
                if(!$userId){
                    return $this->return_result($this->returnData);
                }
                $tempData = SubscriptionModel::getFields("remind_status", ["broadcaster_id" => $item["id"], "user_id" => $userId]);
                if (!empty($tempData)) {
                    $item["subscribe"] = 1; // 已订阅
                    $item["remindStatus"] = intval($tempData["remindStatus"]); // 提醒状态
                }
            }
        }
        $this->returnData["data"] = $broadcasterData;
        return $this->return_result($this->returnData);
    }

    // 订阅或者取消订阅
    public function subscribeBroadcaster(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        if(empty($request->id)){
            return $this->return_result(ErrorCode::code("params_not_exist", "up主ID"));
        }
        $tempData = SubscriptionModel::getFields("id", ["broadcaster_id" => $request->id, "user_id" => $request->userLoginData["id"]]);
        if(!empty($tempData)){
            SubscriptionModel::delById($tempData["id"]);
        }else{
            SubscriptionModel::toAdd(["broadcaster_id" => $request->id, "user_id" => $request->userLoginData["id"]]);
        }
        return $this->return_result($this->returnData);
    }

    // 提醒或者取消提醒
    public function remindBroadcaster(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        if(empty($request->id)){
            return $this->return_result(ErrorCode::code("params_not_exist", "up主ID"));
        }
        $tempData = SubscriptionModel::getFields("id,remind_status", ["broadcaster_id" => $request->id, "user_id" => $request->userLoginData["id"]]);
        if(!empty($tempData)){
            SubscriptionModel::doUpdate(["remind_status" => $tempData['remindStatus'] == '1' ? 0 : 1], ["id" => $tempData["id"]]);
        }else{
            return $this->return_result(ErrorCode::code("customized", "未订阅up主"));
        }
        return $this->return_result($this->returnData);
    }
}
